package controller;


import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import AccessDB.AdministradorDAO;
import AccessDB.AlunoDAO;
import AccessDB.ConnectionDB;
import AccessDB.ProfessorDAO;
import AccessDB.ResponsavelDAO;
import AccessDB.UsuarioDAO;

import model.Administrador;
import model.Aluno;
import model.Professor;
import model.Responsavel;


public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public LoginServlet() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String comando = request.getParameter("comando");
		HttpSession session = request.getSession();
		
		if(comando.equalsIgnoreCase("sair")){
			
			session.invalidate();
			
			RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
			dispatcher.forward(request, response);
			
			return;
			
		}
	
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String comando = request.getParameter("comando");
		
		if(comando.equalsIgnoreCase("login"))
		{
			
			String loginUsuario = request.getParameter("usuario");
			String senha = request.getParameter("senha");
			
			try {
				String tipoUsuario = UsuarioDAO.autenticaUsuario(loginUsuario, senha);
				
				HttpSession session = request.getSession();
				
				if(tipoUsuario != null)
				{
					if(tipoUsuario.equalsIgnoreCase("professor"))
					{
						ProfessorDAO prof = new ProfessorDAO();
						
						Professor usuario = prof.retornaProfessor(loginUsuario);
						
						session.setAttribute("usuario", usuario);
						
						RequestDispatcher dp = request.getRequestDispatcher("professor/index.jsp");
						dp.forward(request, response);	
						return;
					}
					
					//novo - por Luiz Felipe - 13/12/2009 {
					
					else if(tipoUsuario.equalsIgnoreCase("administrador"))
					{
						AdministradorDAO adm = new AdministradorDAO();
						
						Administrador usuario = adm.retornaAdministrador(loginUsuario);
						
						session.setAttribute("admin", usuario);
						
						RequestDispatcher dp = request.getRequestDispatcher("administrador/index.jsp");
						dp.forward(request, response);	
						return;
					}
					
					// } novo - por Luiz Felipe - 13/12/2009
					
					//novo - por Luiz Felipe - 22/12/2009 {
					
					else if(tipoUsuario.equalsIgnoreCase("aluno"))
					{
						AlunoDAO aluno = new AlunoDAO();
						
						Aluno usuario = aluno.retornaAluno(loginUsuario);
						
						session.setAttribute("aluno", usuario);
						
						RequestDispatcher dp = request.getRequestDispatcher("aluno/index.jsp");
						dp.forward(request, response);	
						return;
					}
					else if(tipoUsuario.equalsIgnoreCase("responsavel"))
					{
						Responsavel usuario = new ResponsavelDAO().retornaResponsavel(loginUsuario);
						session.setAttribute("responsavel", usuario);
						RequestDispatcher dp = request.getRequestDispatcher("pais/index.jsp");
						dp.forward(request, response);	
						return;						
						
					}
					
					// } novo - por Luiz Felipe - 22/12/2009
				}
				RequestDispatcher dp = request.getRequestDispatcher("login.jsp");
				dp.forward(request, response);	
				return;
			
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		
	}

}
